<?php
    $openid = $_GET["openid"];
    $a = mysqli_connect("localhost","root","gala123456");
    $db = mysqli_select_db($a,"sheep");
    $sql = "SELECT `avatar`,`admin`,`business` FROM `user` WHERE `openid` = '$openid'";
    $query = mysqli_query($a,$sql);

    $url;
    $admin;
    $business;
    $sheep_num = 0;
    $yzd = array();
    $address = array();
    $sheep_select_num = array();
    $address_img = array();
    $ranking = 1;
    $rankings = array();

    while ($row = mysqli_fetch_row($query)) {
        $ding = 0;
        foreach ($row as $nei) {
            if ($ding ==0) { 
                $url = $nei;
            }else if ($ding == 1) {
                $admin = $nei;
            }else if ($ding == 2) {
                $business = $nei;
            }
            $ding++;
        }
    }
    $sql1 = "SELECT `sheepID`,`business` FROM `sheep` WHERE `sheep_select` = '$openid'";
    $query1 = mysqli_query($a,$sql1);
    while ($row1 = mysqli_fetch_row($query1)) {
        $ding = 0;
        $ding1 = 0;
        $sheep_num++;
        foreach ($row1 as $nei1) {
            if ($ding == 1) {
                count($yzd) == 0?array_push($yzd,$nei1)&&array_push($sheep_select_num,1):$ding1=1;
                if ($ding1 == 1) {
                    $he = null;
                    for ($i = 0;$i < count($yzd);$i++) {
                        if ($yzd[$i] == $nei1) {
                            $he = $i;
                        }
                    }
                    $he == null?array_push($yzd,$nei1)&&array_push($sheep_select_num,1):$sheep_select_num[$he]++;
                }
            }
            $ding++;
        }
    }
    for ($i = 0;$i < count($yzd);$i++) {
        $sql2 = "SELECT `business_address` FROM `business` WHERE `business_name` = '".$yzd[$i]."'";
        $query2 = mysqli_query($a,$sql2);
        while ($row2 = mysqli_fetch_row($query2)) {
            foreach ($row2 as $nei2) {
                array_push($address,$nei2);
            }
        }
        $sql3 = "SELECT `address_img` FROM `address_img` WHERE `name` = '".$address[$i]."'";
        $query3 = mysqli_query($a,$sql3);
        while ($row3 = mysqli_fetch_row($query3)) {
            foreach ($row3 as $nei3) {
                array_push($address_img,$nei3);
            }
        }
    }
    $sql4 = "SELECT `sheep_select` FROM `sheep`";
    $query4 = mysqli_query($a,$sql4);
    while ($row4 = mysqli_fetch_row($query4)) {
        foreach ($row4 as $nei4) {
            if (count($rankings) == 0){
                if ($nei4 == null) {

                }else{
                    $rankings[$nei4] = 1;
                }
            }else{
                $aaa = null;
                foreach ($rankings as $index=>$value) {
                    if ($index == $nei4) {
                        $aaa = $index;
                    }
                    $aaa == null?$rankings[$index] = 1:$rankings[$index]++;
                }
            }
        }
    }
    ksort($rankings);
    foreach ($rankings as $index=>$value) {
        if ($index == $openid) {
            break;
        }else{
            $ranking++;
        }
    }
    if (count($rankings) == 0) {
        $ranking = 1;
    }


    $arr = array(
        "url" => $url,
        "admin" => $admin,
        "business" => $business,
        "sheep_num" => $sheep_num,
        "yzd" => $yzd,
        "address" => $address,
        "sheep_select_num" => $sheep_select_num,
        "address_img" => $address_img,
        "sking" => $ranking
    );
    echo json_encode($arr);
?>